#pragma once

#include<iostream>
#include"InetAddr.hpp"
#include<functional>
#include<memory>
#include"Reactor.hpp"
//fd分为普通的fd和listensockfd
class Connection;
class EpollServer ;
using func_t =std::function<void(std::shared_ptr<Connection> conn)>;

class Connection
{
    public:
    Connection():_sockfd(-1),_events(0),_owner(nullptr)
    {}
    void SetEvents(uint32_t events)
    {
        _events = events;
    }
    uint32_t Events()
    {
        return _events;
    }
    void SetSockfd(int sockfd)
    {
        _sockfd = sockfd;
    }
    void SetPeerInfo(InetAddr peer_addr)
    {
        _peer_addr = peer_addr;
    }
    int Fd()
    {
        return _sockfd;
    }
    void SetOwner(Reactor* owner)
    {
        _owner = owner;
    }
    Reactor* Owner()
    {
        return _owner;
    }
    void Close()
    {
        if(_sockfd>=0)
        close(_sockfd);
    }
    ~Connection(){}
    virtual void Recever()=0;
virtual void Sender()=0;
virtual void Excepter()=0;//异常处理方法
protected:
int _sockfd;
std::string _outbuff;
std::string _inbuff;
InetAddr _peer_addr;//对应连接的哪个客户端
uint32_t _events;


Reactor* _owner;//该链接connect所属的E

};

class Factory
{
public:
static std::shared_ptr<Connection> BulidConnection(int fd,uint32_t events,func_t recever,func_t sender,func_t excepter)
{
auto con= std::make_shared<Connection>(fd);
con->SetEvents(events);
}
private:
};